//
//  VRCommitTableViewCell.swift
//  VoiceRoom
//
//  Created by Rui on 2024/3/30.
//

import UIKit

class VRCommitTableViewCell: UITableViewCell {

    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(16).end()
    private let nameLabel          = c_make_ex(UILabel.self)
        .color(.white).font(14.font_semiBold).text("Janank").end()
    private let timeLabel          = c_make_ex(UILabel.self)
        .color(.white.alpha(0.3)).font(12.font_medium)
        .text("1 hour ago").end()
    private let moreButton          = c_make_ex(UIButton.self)
        .image("Commit_more").end()
    private let contentLabel          = c_make_ex(UILabel.self)
        .color(.white).font(14.font_regular).line(0)
        .text("The first time I came here, I felt good and I will come again").end()
    
    var model:InfoUtil.commentDta?{
        didSet{
            guard let md = model else {
                return
            }
            iconImageView.loadImage(url: md.userImage)
            nameLabel.text = md.userName
            contentLabel.text = md.msg
            timeLabel.text = md.createTime
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews {
            iconImageView
            nameLabel
            timeLabel
            moreButton
            contentLabel
        }
        
        moreButton.size(40).trailing(5).top(0)
        iconImageView.leading(15).top(1).size(32)
        nameLabel.leading(52).trailing(60).top(1.5)
        timeLabel.leading(52)
        timeLabel.Top == nameLabel.Bottom
        contentLabel.leading(15).trailing(15).bottom(15)
        contentLabel.Top == iconImageView.Bottom + 10
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}


class VRCommitDiscoverCell: UITableViewCell {

    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(20).end()
    private let nameLab = c_make_ex(UILabel.self)
        .text("name").color(.white).font(14.font_bold).end()
    private let timeLab = c_make_ex(UILabel.self)
        .text("11.09 12:31").color(.white.alpha(0.4))
        .font(12.font_medium).end()
    private let followBtn = c_make_ex(UIButton.self)
        .text("Follow").text("Unfollow",state: .selected)
        .color("#FFFC00").color(.white.alpha(0.4),state: .selected)
        .font(12.font_semiBold).corner(12)
        .border(.white.alpha(0.4),width: 1).end()
    private let contentLab = c_make_ex(UILabel.self)
        .text("A huge tree that fills one’s arms grows from a tiny seedling; a nine-storied tower rises Irom a heap of earth; a thousand li journey starts with th…")
        .color(.white).font(14.font_medium).line(0).end()
    private let oneImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(10).end()
    private let twoImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(10).end()
    private var oneTag = UIIconButton(style: .leading(itemSpace: 5))
    private var twoTag = UIIconButton(style: .leading(itemSpace: 5))
    private var threeTag = UIIconButton(style: .leading(itemSpace: 5))
    private let commitButton = UIIconButton(style: .leading(itemSpace: 5))
    private let likeButton = UIIconButton(style: .leading(itemSpace: 5))
    private let commentLab = c_make_ex(UILabel.self)
        .text("Comment(1234)").color(.white).font(16.font_medium).end()
    
    var roomModel:InfoUtil.room?{
        didSet{
            guard let md = roomModel else { return }
            iconImageView.loadImage(url: md.info1)
            nameLab.text = md.info2
            timeLab.text = md.info3
            contentLab.text = md.descInfo
            if let icon = md.icon, icon.count > 0 {
                let imgArray = icon.components(separatedBy: ",")
                oneImageView.isHidden = false
                oneImageView.loadImage(url: imgArray.first)
                if imgArray.count > 1 {
                    twoImageView.isHidden = false
                    twoImageView.loadImage(url: imgArray.last)
                }else{
                    twoImageView.isHidden = true
                }
            }
            if let tags = md.info, tags.count > 0 {
                let tagArray = tags.components(separatedBy: ",")
                if tagArray.count == 1 {
                    oneTag.isHidden = false
                    twoTag.isHidden = true
                    threeTag.isHidden = true
                    oneTag.setTitle(tagArray.first, for: .normal)
                    var w = (tagArray.first! as NSString).size(withAttributes: [NSAttributedString.Key.font: 12.font_semiBold!]).width
                    w = w + 35.0
                    oneTag.widthConstraint?.constant = w
                    
                } else if tagArray.count == 2 {
                    oneTag.isHidden = false
                    twoTag.isHidden = false
                    threeTag.isHidden = true
                    oneTag.setTitle(tagArray.first, for: .normal)
                    twoTag.setTitle(tagArray[1], for: .normal)
                    var w1 = (tagArray.first! as NSString).size(withAttributes: [NSAttributedString.Key.font: 12.font_semiBold!]).width
                    w1 = w1 + 35.0
                    oneTag.widthConstraint?.constant = w1
                    var w2 = (tagArray[1] as NSString).size(withAttributes: [NSAttributedString.Key.font: 12.font_semiBold!]).width
                    w2 = w2 + 35.0
                    twoTag.widthConstraint?.constant = w2
                } else if tagArray.count == 2 {
                    oneTag.isHidden = false
                    twoTag.isHidden = false
                    threeTag.isHidden = false
                    oneTag.setTitle(tagArray.first, for: .normal)
                    twoTag.setTitle(tagArray[1], for: .normal)
                    threeTag.setTitle(tagArray.last, for: .normal)
                    var w1 = (tagArray.first! as NSString).size(withAttributes: [NSAttributedString.Key.font: 12.font_semiBold!]).width
                    w1 = w1 + 35.0
                    oneTag.widthConstraint?.constant = w1
                    var w2 = (tagArray[1] as NSString).size(withAttributes: [NSAttributedString.Key.font: 12.font_semiBold!]).width
                    w2 = w2 + 35.0
                    twoTag.widthConstraint?.constant = w2
                    var w3 = (tagArray.last! as NSString).size(withAttributes: [NSAttributedString.Key.font: 12.font_semiBold!]).width
                    w3 = w3 + 35.0
                    threeTag.widthConstraint?.constant = w3
                }
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews {
            iconImageView
            nameLab
            timeLab
            followBtn
            contentLab
            oneImageView
            twoImageView
            oneTag
            twoTag
            threeTag
            commitButton
            likeButton
            commentLab
        }
        
        iconImageView.leading(15).top(20).size(40)
        nameLab.Top == iconImageView.Top
        nameLab.Leading == iconImageView.Trailing + 10
        timeLab.Leading == nameLab.Leading
        timeLab.Top == nameLab.Bottom + 3
        contentLab.leading(15).trailing(15)
        contentLab.Top == iconImageView.Bottom + 10
        followBtn.trailing(15).height(24).top(23)
        oneImageView.leading(15).size(99)
        oneImageView.Top == contentLab.Bottom + 10
        twoImageView.leading(125).size(99)
        twoImageView.CenterY == oneImageView.CenterY
        oneTag.leading(15).height(24).width(75)
        oneTag.Top == oneImageView.Bottom + 15
        twoTag.height(24).width(75)
        twoTag.Leading == oneTag.Trailing + 15
        twoTag.CenterY == oneTag.CenterY
        threeTag.height(24).width(75)
        threeTag.Leading == twoTag.Trailing + 15
        threeTag.CenterY == twoTag.CenterY
        commitButton.leading(15)
        commitButton.Top == oneTag.Bottom + 20
        likeButton.leading(90)
        likeButton.CenterY == commitButton.CenterY
        commentLab.leading(15).bottom(15)
        commentLab.Top == commitButton.Bottom + 20
        
        updateTagButton(button: oneTag)
        updateTagButton(button: twoTag)
        updateTagButton(button: threeTag)
        
        followBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        
        commitButton.style { v in
            v.contentHorizontalAlignment = .left
            v.setImage("Discover_commit".toImage, for: .normal)
            v.setTitle("1234", for: .normal)
            v.iconSize = CGSize(width: 22, height: 22)
            v.titleLabel?.font = 14.font_medium
            v.setTitleColor(.white.alpha(0.6), for: .normal)
            v.isUserInteractionEnabled = false
        }
        
        likeButton.style { v in
            v.contentHorizontalAlignment = .left
            v.setImage("Discover_like".toImage, for: .normal)
            v.setTitle("1234", for: .normal)
            v.iconSize = CGSize(width: 22, height: 22)
            v.titleLabel?.font = 14.font_medium
            v.setTitleColor(.white.alpha(0.6), for: .normal)
            v.isUserInteractionEnabled = false
        }
    }
    
    func updateTagButton(button: UIIconButton){
        
        button.layer.cornerRadius = 12
        button.layer.masksToBounds = true
        button.setImage("Discover_tag".toImage, for: .normal)
        button.iconSize = CGSize(width: 12, height: 12)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = 12.font_semiBold
        button.isUserInteractionEnabled = false
        button.backgroundColor = .white.alpha(0.1)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
